<?php
/**
 * 
 * 夜店 处理类
 * @author tanzhe
 *
 */
class ClubModel {
	private $db;
	private $cache;
	private $clubRequire;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
		global $clubRequire;
		$this->clubRequire = $clubRequire;
	}
	
	/**
	 * 
	 * 进入夜店
	 * @param str $key
	 * @param str $label
	 * @param str $type
	 */
	public function enter($key, $label, $type=0) { 
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		//判断是否满足进入条件 
		if ( isset($this->clubRequire[$label]) ) {
			if ( $this->clubRequire[$label]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
			if ( $this->clubRequire[$label]['club_hotness'] > $playerInfo['club_hotness'] ) {
				return array('code'=>212,'ret'=>'');
			}
		} else {
			return array('code'=>222,'ret'=>'');
		}
		if ( empty($type) ) {
			return array('code'=>0,'ret'=>array('success'=>1));	
		}
		if ( isset($this->clubRequire[$label]['data'][$type]) ) {
			$data = $this->clubRequire[$label]['data'][$type];
			$randBoy = array_rand($data, 3);
			$boyfriend = array();
			foreach ( $randBoy as $v ) {
				$boyfriend[] = $data[$v];
			}
			return array('code'=>0,'ret'=>array('boyfirend'=>$boyfriend));
		} else {
			return array('code'=>222,'ret'=>'');
		}
	}

	/**
	 * 
	 * 调情
	 * @param $key
	 * @param $label
	 * @param $type
	 * @param $boy
	 * @param $item
	 */
	function flirt($key, $label, $type, $boy, $item) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		//判断现在是否已经有男朋友
		if ( !empty($playerInfo['boyfriend_id']) ) {
			return array('code'=>224,'ret'=>''); 
		} 
		//判断是否满足进入条件 
		if ( isset($this->clubRequire[$label]) ) {
			if ( $this->clubRequire[$label]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
			if ( $this->clubRequire[$label]['club_hotness'] > $playerInfo['club_hotness'] ) {
				return array('code'=>212,'ret'=>'');
			}
		} else {
			return array('code'=>222,'ret'=>'');
		}
		if ( isset($this->clubRequire[$label]['data'][$type]) ) {
			$data = $this->clubRequire[$label]['data'][$type];
			if ( !in_array($boy, $data) ) {
				return array('code'=>223,'ret'=>'');
			}
		} else {
			return array('code'=>222,'ret'=>'');
		}
		//判断道具能否使用
		if ( !in_array($item, $this->clubRequire[$label]['item']) ) {
			return array('code'=>214,'ret'=>'');
		}
		//获取道具信息
		if ( !empty($item) ) {
			$itemData = $this->cache->get('itemdata_'.$item);
			if ( empty($itemData) ) {
				$query = $this->db->query("select * from item where `label`='{$item}'");
				$itemData = $this->db->fetchOneByQuery($query);
				$this->cache->set('itemdata_'.$item, $itemData);
			}
			if ( empty($itemData) ) { //不存在该道具
				return array('code'=>213,'ret'=>'');
			}
		} else {
			return array('code'=>213,'ret'=>'');
		}
		//判断钱是否足够
		if ( $itemData['cost_type'] == 1 ) {
			if ( $itemData['price'] > $playerInfo['coin'] ) {
				return array('code'=>9,'ret'=>'');
			}
		} else {
			if ( $itemData['price'] > $playerInfo['cash'] ) {
				return array('code'=>10,'ret'=>'');
			}
		}
		//获取男朋友模板信息
		$boyFriendData = $this->cache->get('boy_template_'.$boy);
		$boyFriendData = array();
		if ( empty($boyFriendData) ) {
			$query = $this->db->query("select * from boyfriend where `label`='{$boy}'");
			$boyFriendData = $this->db->fetchOneByQuery($query);
			$this->cache->set('boy_template_'.$boy, $boyFriendData);
		}
		if ( empty($boyFriendData) ) { //不存在该男朋友
			return array('code'=>225,'ret'=>'');
		}
		//成功率
		$isSuccess = GameLogic::flirtResult($playerInfo['club_hotness'], $boyFriendData['quality'], $itemData['bonus']);
		$updateData = array();
		if ( $isSuccess ) {
			//插入男朋友信息表
			$insertData = array(
				'u_id'=>$playerInfo['u_id'],
				'boyfriend'=>$boy,
				'name'=>$boyFriendData['name'],
				'quality'=>$boyFriendData['quality'],
				'job'=>$boyFriendData['job'],
				'dated'=>0,
				'favor'=>0,
				'mood'=>100,
				'clothes'=>$boyFriendData['clothes'],
				'style'=>$boyFriendData['style'],
				'color'=>$boyFriendData['color'],
				'skin'=>$boyFriendData['skin'],
				'bonus'=>$boyFriendData['bonus'],
				'manliness'=>$boyFriendData['manliness'],
				'add_time'=>time(),
				'update_time'=>time(),
			);
			$this->db->insertData('player_boyfriend', $insertData);
			$newBoyId = $this->db->insert_id();
			//计算buff
			$itemModel = new ItemModel();
			$itemModel->addBuff(0, $boyFriendData['bonus'], 0 ,$playerInfo);
			$updateData['boyfriend_id'] = $newBoyId;
		}
		
		//更新角色表
		if ( $itemData['cost_type'] == 1 ) {
			$updateData['coin'] = $playerInfo['coin'] - $itemData['price'];
		} else {
			$updateData['cash'] = $playerInfo['cash'] - $itemData['price'];
		}
		$playerInfo = array_merge($playerInfo, $updateData);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		//写元宝log
		if ( isset($updateData['cash']) ) {
			$aryData = array(
				'label'=>$label,
				'item'=>$item,
				'type'=>$type,
				'boy'=>$boy,
				'boy_id'=>isset($newBoyId) ? $newBoyId : 0,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'flirt',
				'num'=> -$itemData['price'],
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $playerInfo['next_energy_time'] - time();
			
		}		
		$this->cache->set("ku_".$key, $playerInfo);
		//返回个人信息
		$ret = array(
			'success'=>$isSuccess == true ? 1 : 0,
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'hanger'=>$playerInfo['hanger'],
			'count_down'=>$countDown,
			'coin_factor'=>$playerInfo['coin_factor'],
			'hanger_factor'=>$playerInfo['hanger_factor'],
			'cd_factor'=>$playerInfo['cd_factor'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	
}
?>